package com.android.videotest.video;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.android.videotest.App;
import com.android.videotest.DimensionUtils;
import com.android.videotest.R;
import com.android.videotest.music.ui.playlist.PlayListDialog;
import com.android.videotest.video.data.VideoInfoHelper;
import com.bumptech.glide.Glide;

import java.io.File;

public class VideoActivity extends AppCompatActivity {

    public static final String PATH = "path";
    VideoWrapper mVideoWrapper;
    FrameLayout mVideoContainer;
    private boolean mPlayToNextPage;

    public static void start(Context context, String path) {
        Intent starter = new Intent(context, VideoActivity.class);
        starter.putExtra(PATH, path);
        context.startActivity(starter);
    }
    File file = new File(Environment.getExternalStorageDirectory(), "/1/花月成双.mp4");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        String path = getIntent().getStringExtra("path");
        mVideoContainer = findViewById(R.id.contentPanel);

        mVideoWrapper = VideoWrapper.getInstance(App.getApp());
        ViewGroup parent = (ViewGroup) mVideoWrapper.getParent();
        if (parent != null) {
            parent.removeView(mVideoWrapper);
        }
        mVideoContainer.addView(mVideoWrapper, DimensionUtils.getScreenWidth(), (int) (DimensionUtils.getScreenWidth() / 1.5));
        mVideoWrapper.setActivity(this);
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                String url_1 = "http://baobab.kaiyanapp.com/api/v1/playUrl?vid=188347&resourceType=video&editionType=default&source=aliyun&playUrlType=url_oss";
//                String url_1 = "https://b2cimg.htd.cn/20191028104102_条码秤规则.mp4";
//                String url_1 = "http://mgcdn.vod.migucloud.com/vi1/352.0qvEdy5kldsFhtCh21HUba.1.3sih7P.mp4";
                mVideoWrapper.loadUri(Uri.fromFile(file));
                mVideoWrapper.openVideo();
                mVideoWrapper.start();
            }
        });
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url_1 = "sdcard/qqmusic/mv/九尾 - 朱一龙、周冬雨、马思纯、李现、李沁《你好2020》_012ZPcFP1fVhRP_3_0 [mqms].mp4";
                mVideoWrapper.loadUrl(url_1);
                mVideoWrapper.openVideo();
                mVideoWrapper.start();
            }
        });
        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url_1 = "http://mgcdn.vod.migucloud.com/vi1/352.3pJTeObLRfdqIPI2YiDm0.4.7L7Yg5.mp4";
                mVideoWrapper.loadUrl(url_1);
                mVideoWrapper.openVideo();
                mVideoWrapper.start();
            }
        });

//        View iv_jump = findViewById(R.id.iv_jump);
//        iv_jump.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mPlayToNextPage = true;
//                VideoDetailActivity.start(VideoActivity.this);
//            }
//        });

//        File file = new File("sdcard/");
//        String[] list = file.list();
//        for (int i = 0; i < list.length; i++) {
//            Log.d(this.getClass().getSimpleName() + "-App", "onCreate: " + list[i]);
//        }
//        seekBar.setMax(100);
//        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                if (fromUser) {
//                    Log.d(this.getClass().getSimpleName() + "-App", "onProgressChanged() called with: seekBar = [" + seekBar + "], progress = [" + progress + "], fromUser = [" + fromUser + "]");
//                    int duration = videoView.getDuration();
//                    float progressTime = duration * (progress / 100f) ;
//                    Log.d(this.getClass().getSimpleName() + "-App", "onProgressChanged: progressTime  " + progressTime);
//                    videoView.seekTo((int) progressTime);
//                }
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//                Log.d(this.getClass().getSimpleName() + "-App", "onStartTrackingTouch() called with: seekBar = [" + seekBar + "]");
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//                Log.d(this.getClass().getSimpleName() + "-App", "onStopTrackingTouch() called with: seekBar = [" + seekBar + "]");
//            }
//        });
//        Uri uri = Uri.parse("android.resource://com.android.videotest/" + R.raw.test_vide);
//        videoView.loadUri(uri);
//        String url_1 = "http://baobab.kaiyanapp.com/api/v1/playUrl?vid=188347&resourceType=video&editionType=default&source=aliyun&playUrlType=url_oss";

//        mVideoWrapper.loadUrl("http://baobab.kaiyanapp.com/api/v1/playUrl?vid=188347&resourceType=video&editionType=default&source=aliyun&playUrlType=url_oss");
//        mVideoWrapper.loadUri(Uri.parse("sdcard/qqmusic/mv/九尾 - 朱一龙、周冬雨、马思纯、李现、李沁《你好2020》_012ZPcFP1fVhRP_3_0 [mqms].mp4"));
//        mVideoWrapper.loadUri(Uri.parse("/storage/emulated/0/DCIM/Camera/VID_20200313_141200.mp4"));
//        videoView.loadUrl("https://tv.miguvideo.com/?246745950&room20200225163023725_R2&24674595020200225163023427&channelId=201800000010059&timepstamp=1582619428586&userId=246745950&clientId=05ci4651677867661049565410#/video/live/246745950/room20200225163023725_R2");
//        videoView.loadUrl("http://mgilvbpull.migucloud.com/352/LZGPX0MN_C0_3.m3u8");
//        mVideoWrapper.openVideo();
//getContentResolver().openFileDescriptor()
//        ContentResolver.openFileDescriptor
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onPause() {
        super.onPause();
        mVideoWrapper.pause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mVideoWrapper.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mVideoWrapper != null && !mPlayToNextPage) {
            mVideoWrapper.onDestroy();
        }
    }

}
